﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.IO;

namespace NCLCustomerManagement.UI.Helper
{
    public class PartialViewRender : IPartialViewRender
    {
        private string ValidationSummaryViewPath = "~/Views/Shared/_ValidationSummary.cshtml";

        private JavaScriptSerializer _jsSerializer;

        public PartialViewRender(JavaScriptSerializer jsSerializer)
        {
            _jsSerializer = jsSerializer;
        }

        public string RenderValidationSummaryView(ControllerContext controllerContext,
            ViewDataDictionary viewData, TempDataDictionary tempData, object model)
        {
            viewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controllerContext, ValidationSummaryViewPath);
                ViewContext viewContext = new ViewContext(controllerContext, viewResult.View, viewData, tempData, sw);
                viewResult.View.Render(viewContext, sw);

                return _jsSerializer.Serialize(sw.GetStringBuilder().ToString());
            }
        }
    }
}